<?php
namespace app\restaurant\model;

use app\BaseModel;
use think\facade\Db;

class StoreStaff extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_restaurant_store_staff';


    /**
     * @author chenniang
     * @DataTime: 2020-10-12 11:02
     * @功能说明:添加
     */
    public function dataAdd($data,$uniacid){

        foreach ($data['staff'] as $k => $v){

            $data['staff'][$k]['uniacid']  = $uniacid;

            $data['staff'][$k]['store_id'] = $data['store_id'];

            $data['staff'][$k]['status']   = 1;

            $data['staff'][$k]['create_time'] = time();

        }

        $this->saveAll($data['staff']);

        return true;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-12 11:24
     * @功能说明:修改
     */
    public function staffUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }

    /**
     * @author chenniang
     * @DataTime: 2020-10-12 11:26
     * @功能说明:删除
     */
    public function staffDel($dis){

        $res = $this->where($dis)->delete();

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:门店添加
     */
    public function staffAdd($data){

        $data['create_time'] = time();

        $data['status'] = 1;

        $res = $this->insert($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-02 17:14
     * @功能说明:员工下拉
     */
    public function staffSelect($dis){

        $data = $this->alias('a')
            ->join('longbing_card_user_info b','a.staff_id = b.fans_id','left')
            ->where($dis)
            ->field(['b.name as staff_name','b.avatar','b.phone','b.company_id','a.*'])
            ->group('a.id')
            ->order('a.id desc')
            ->select()
            ->toArray();

        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-10 16:41
     * @功能说明:列表
     */

    public function staffList($dis,$where,$page,$uniacid){


        $data = $this->alias('a')
                ->join('longbing_card_user_info b','a.staff_id = b.fans_id','left')
//                ->join('longbing_card_user_phone c','c.user_id = a.staff_id','left')
                ->join('longbing_card_user d','a.staff_id = d.id','left')
                ->where($dis)
                ->where(function ($query) use ($where){
                    $query->whereOr($where);
                })
                ->field(['b.name as staff_name','d.nickName','b.avatar','b.phone','b.company_id','a.*'])
                ->group('a.id')
                ->order('a.id desc')
                ->paginate($page)
                ->toArray();

        $data['data'] = transImagesOne($data['data'],['avatar'],$uniacid);

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

               $v['now_role']    = $this->where(['store_id'=>$v['store_id'],'role'=>1])->value('staff_id');

               $v['now_role']    = Db::name('longbing_card_user_info')->where(['fans_id'=>$v['now_role']])->value('name');

               $company_arr      = getCompanyAndTop($v['company_id']);
                //公司
               $v['company']     = $company_arr['company'];
                //部门
               $v['son_company'] = $company_arr['son_company'];

               $v['avatar']      = !empty($v['avatar'])?$v['avatar']:lbUserAvatar($v['staff_id']);

//               $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);

            }

        }

        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-05 14:12
     * @功能说明:员工业绩排名
     */
    public function orderCountTopList($dis){

        $data = $this->alias('a')
                ->join('longbing_card_user_info b','a.staff_id = b.fans_id','left')
                ->join('longbing_card_restaurant_order e','a.staff_id = e.staff_id','left')
                ->join('longbing_card_job f','f.id = b.job_id','left')
                ->where($dis)
                ->field(['b.name as staff_name','b.avatar','count(e.id) as order_count','a.id','a.staff_id','f.name as job'])
                ->group('a.staff_id')
                ->order('order_count desc, a.id desc')
                ->paginate(10)
                ->toArray();

        return $data;
    }

    /**
     * @author chenniang
     * @DataTime: 2021-03-05 14:12
     * @功能说明:员工业绩排名
     */
    public function orderCountTopThree($dis,$limit=3){

        $data = $this->alias('a')
            ->join('longbing_card_user_info b','a.staff_id = b.fans_id','left')
            ->join('longbing_card_restaurant_order e','a.staff_id = e.staff_id','left')
            ->where($dis)
            ->field(['b.name as staff_name','b.avatar','count(e.id) as order_count','a.id','a.staff_id'])
            ->group('a.staff_id')
            ->order('order_count desc, a.id desc')
            ->limit($limit)
            ->select()
            ->toArray();


        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-20 09:42
     * @功能说明:获取员工的门店
     */
    public function getStaffStore($user_id,$role=3){

        $dis = [

            'staff_id' => $user_id,

            'status'   => 1
        ];

        if($role!=3){

            $dis['role'] = $role;
        }

        $store = $this->where($dis)->find();

        return !empty($store)?$store->toArray():[];

    }

    /**
     * @author chenniang
     * @DataTime: 2020-12-02 17:00
     * @功能说明:
     */
    public function getStaffId($store_id){

        $dis = [

            'store_id' => $store_id,

            'status'   => 1,

            'role'     => 1
        ];

        $store = $this->where($dis)->value('staff_id');

        return $store;

    }












}